package com.rocksdata.mc.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.support.v7.widget.RecyclerView;

import com.ctz.commonframe.ui.WebViewActivity;
import com.ctz.commonframe.utils.FullyLinearLayoutManager;
import com.ctz.commonframe.utils.RecycleViewDivider;
import com.rocksdata.mc.BuildConfig;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.AppWidgetInfo;
import com.rocksdata.mc.entity.NewsInfo2;
import com.rocksdata.mc.ui.adapter.WidgetNewsRecyclerAdapter;
import com.rocksdata.mc.R;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/5/12.
 */

public class NewsWidgetView extends BaseWidgetView {
    private RecyclerView rvNews;
    private List<NewsInfo2> newsList;
    private WidgetNewsRecyclerAdapter adapter;

    public NewsWidgetView(Context context) {
        super(context);
    }

    public NewsWidgetView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setupHeadView() {
        ivIcon.setImageResource(R.mipmap.ic_widget_news);
        tvName.setText(getResources().getString(R.string.news));
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onEnterAppListener != null){
                    onEnterAppListener.onEnterApp(AppWidgetInfo.TYPE_NEWS);
                }
            }
        };
        ivIcon.setOnClickListener(listener);
        tvName.setOnClickListener(listener);
    }

    @Override
    public View getContentView() {
        @SuppressLint("InflateParams")
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.widget_news2, null);
        rvNews = contentView.findViewById(R.id.rv_news);
        FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        rvNews.setNestedScrollingEnabled(false);
        rvNews.setLayoutManager(linearLayoutManager);
        rvNews.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, 2, getResources().getColor(R.color.light_gray)));
        rvNews.setFocusable(false);
        return contentView;
    }

    @Override
    public void bindData() {
        if(newsList == null)
            newsList = new ArrayList<>();
        newsList.clear();
        BusinessManager.getNewsManager().getNewsListByTag(null, 1, new BusinessInterface.OnBusinessResultListener() {
            @Override
            public void onBusinessResult(boolean isSuccess, Object object, String message) {
                if(isSuccess){
                    @SuppressWarnings("unchecked")
                    List<NewsInfo2> list = (List<NewsInfo2>) object;
                    if(list != null && list.size() > 0) {
                        for(NewsInfo2 info: list){
                            if(!newsList.contains(info))
                                newsList.add(info);
                        }
                    }
                }
                if(adapter == null) {
                    adapter = new WidgetNewsRecyclerAdapter(mContext, newsList);
                    rvNews.setAdapter(adapter);
                    adapter.setOnItemClickListener(new WidgetNewsRecyclerAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(Object item) {
                            NewsInfo2 info = (NewsInfo2) item;
                            String url;
                            switch (info.getType()){
                                case NewsInfo2.NEWS_TYPE_VIDEO:
                                    url = "file:///android_asset/pages/video.html?id=" + info.getNewsCode();
                                    break;
                                default:
                                    url = "file:///android_asset/pages/news.html?id=" + info.getNewsCode();
                                    break;

                            }
                            try {
                                url = "file:///android_asset/www/index.html?baseUrl="
                                        + URLEncoder.encode(BuildConfig.HOST, "UTF-8")
                                        + "&apiToken=" + UserInfoManager.getAccessToken()
                                        + "&userType=" + UserInfoManager.getCurrentUserType()
                                        + "&userId=" + UserInfoManager.getCurrentUserId()
                                        + "#/news/" + info.getNewsCode();
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            Intent intent = new Intent(mContext, WebViewActivity.class);
                            intent.putExtra(WebViewActivity.EXTRA_KEY_URL, url);
                            mContext.startActivity(intent);
                        }
                    });
                }else {
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void release() {

    }
}
